package com.jia.lidao.ui.message;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ProgressBar;


import com.jia.lidao.R;
import com.jia.lidao.UserUtils;
import com.jia.lidao.enity.Message;
import com.jia.lidao.network.http.BusinessManager;
import com.jia.lidao.network.http.MyCallback;
import com.jia.lidao.network.http.request.impl.MessageCategoryRequest;
import com.jia.lidao.ui.order.ConfirmOrderActivity;
import com.jia.lidao.widget.CommonTitleBar;

import java.util.List;

import me.leefeng.lfrecyclerview.LFRecyclerView;

/**
 * Created by 00029935 on 2017/11/17.
 */

public class MessageNoticeListFragment extends BaseFragment {

    private CommonTitleBar mCommonTitleBar;
    private LFRecyclerView recyclerView;
    private ProgressBar loading;
    private MessageNoticeAdapter adapter;

    @Override
    protected int getInflateLayout() {
        return R.layout.common_fragment_layout2;
    }

    @Override
    protected void setUpView(View view) {
        loading = (ProgressBar) view.findViewById(R.id.loading);
        recyclerView = (LFRecyclerView) view.findViewById(R.id.recyclerView);

        mCommonTitleBar = (CommonTitleBar) view
                .findViewById(R.id.common_title_bar);
        mCommonTitleBar.setTitle("消息通知");

        recyclerView.setLoadMore(false);
        recyclerView.setRefresh(true);
        adapter = new MessageNoticeAdapter(getContext());
        recyclerView.setAdapter(adapter);
        recyclerView.setLFRecyclerViewListener(
                new LFRecyclerView.LFRecyclerViewListener() {
                    @Override
                    public void onRefresh() {
                        queryData(true);
                    }

                    @Override
                    public void onLoadMore() {

                    }
                });

        adapter.setOnItemClickListener(
                new QuickAdapter.OnItemClickListener<Message.CategoryBean>() {
                    @Override
                    public void OnItemClick(Message.CategoryBean data,
                                            int postion) {
                        // type (string, optional):
                        // 消息类型: 1农资贷 2优惠券 3订单消息 4系统消息

                        if (data.getType() == 4) {
                            Intent intent3 = new Intent(getContext(),
                                    SystemMessageActivity.class);
                            intent3.putExtra("object", data);
                            startActivity(intent3);
                        } else {
                            Intent it = new Intent(getContext(),
                                    MessageOneActivity.class);
                            needrefresh = true;
                            it.putExtra("object", data);
                            startActivity(it);
                        }
                    }
                });
        queryData(true);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // TODO: inflate a fragment view
        View rootView = super.onCreateView(inflater, container,
                savedInstanceState);
        return rootView;
    }

    private void queryData(final Boolean isReflush) {
        if (isFirstIn) {
            isFirstIn = false;
            loading.setVisibility(View.VISIBLE);
        }

        BusinessManager.getInstance().getMessageCategory(
                new MessageCategoryRequest(), new MyCallback<Message>() {
                    @Override
                    public void onSuccess(Message result, String message) {
                        if (result == null || result.getCategory() == null) {
                            return;
                        }
                        List<Message.CategoryBean> dataBeen = result
                                .getCategory();
                        if (null != dataBeen && !dataBeen.isEmpty()) {
                            adapter.reflashData(dataBeen);
                        }
                        needrefresh=false;
                    }

                    @Override
                    public void onError(String errorCode, String message) {
                        showMsgToast(message);
                    }

                    @Override
                    public void onFinished() {
                        if (null != loading) {
                            loading.setVisibility(View.GONE);
                        }

                        if (null != recyclerView) {
                            recyclerView.stopLoadMore();
                            recyclerView.stopRefresh(true);
                        }
                    }

                    @Override
                    public void onRefreshToken() {
                        UserUtils.getInstance().goToLogin(getActivity());
                    }
                });
    }

    public static boolean needrefresh = false;

    @Override
    public void onResume() {
        super.onResume();
        if (needrefresh) {
            queryData(false);
        }
    }
}
